import UIKit
import SnapKit

class StatusCell: UITableViewCell {
    
    var TopConstraints: Constraint?
    //添加模型属性
    var status: Status? {
        didSet {
            //设置原创微博的数据模型
            StatusOriginalView.status = status
//            print(status?.imageURLs)
            //给转发微博设置数据模型
            self.TopConstraints?.uninstall()
            if let re = status?.retweeted_status {
                //有转发微博
                statusRetweetedView.retweetedStatus = re
                
                StatusBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.TopConstraints = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                    statusRetweetedView.hidden = false
                })
            }else {
                //原创微博
                StatusBottomView.snp_updateConstraints(closure: { (make) -> Void in
                    self.TopConstraints = make.top.equalTo(StatusOriginalView.snp_bottom).constraint
                    
                    statusRetweetedView.hidden = true
                })
            }
        }
    }
    
    //cell的默认的构造方法
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        //设置Cell不可选中
        selectionStyle = .None
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        //添加子视图  需要用contentView 来添加
        contentView.addSubview(StatusOriginalView)
        contentView.addSubview(StatusBottomView)
        contentView.addSubview(statusRetweetedView)
        //设置约束
        StatusOriginalView.snp_makeConstraints { (make) -> Void in
            make.left.equalTo(contentView.snp_left)
            make.top.equalTo(contentView.snp_top)
            make.right.equalTo(contentView.snp_right)
            //设置高度
//            make.height.equalTo(80)
        }
        StatusBottomView.snp_makeConstraints { (make) -> Void in
            self.TopConstraints = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            //设置高度
            make.height.equalTo(35)
        }
        statusRetweetedView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(StatusOriginalView.snp_bottom)
            make.left.equalTo(StatusOriginalView.snp_left)
            make.right.equalTo(StatusOriginalView.snp_right)
        }

        //给contentView添加约束
        contentView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self.snp_top) 
            make.left.equalTo(self.snp_left)
            make.right.equalTo(self.snp_right)
            //最关键的约束
            make.bottom.equalTo(StatusBottomView.snp_bottom)
        }
        

    }
    //懒加载子视图
    private lazy var StatusOriginalView: StatusCellOriginalView = StatusCellOriginalView()
    
    //转发微博部分视图
    private lazy var statusRetweetedView: StatusRetweetedView = StatusRetweetedView()
    //底部视图
    private lazy var StatusBottomView: StatusCellBottomView = StatusCellBottomView()
}
